﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN.Admin
{
    public partial class QL_KQthi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["quyen"] != null)
            {
                if (Session["quyen"].ToString() == "AD")
                {

                    Response.Redirect("DangnhapAdmin.aspx");

                }
                else
                {
                    if (Session["Tendangnhap"] != null && Session["Tendangnhap"].ToString() != "")
                    {
                        MasterPage mt = this.Master as MasterPage;
                        LinkButton lb = mt.FindControl("LinkButton1") as LinkButton;
                        lb.Visible = false;
                        Label bl = mt.FindControl("lblDnthanhcong") as Label;
                        bl.Visible = true;
                        Label blten = mt.FindControl("lbtendn") as Label;
                        blten.Visible = true;
                        blten.Text = Session["Tendangnhap"].ToString();
                        LinkButton lbDangky = mt.FindControl("lbDangky") as LinkButton;
                        lbDangky.Visible = false;
                        LinkButton lbthoat = mt.FindControl("lbThoat") as LinkButton;
                        lbthoat.Visible = true;

                    }
                    if (!IsPostBack)
                    {
                        string magv = Session["MaTK"].ToString();
                        Napmonhoc(magv);
                        string monhoc = drMonhoc.SelectedValue;
                    }
                }
            }
            else
            {
                Response.Redirect("LoginAdmin.aspx");
            }
        }
        // Nap mon theo giao vien
        private void Napmonhoc(string magv)
        {
            DataTable dt = new DataTable();
            dt = new CT_MonhocBL().GetAllByUser(magv);
            try
            {
                drMonhoc.DataSource = dt;
                drMonhoc.DataValueField = "Mamonhoc";
                drMonhoc.DataTextField = "Tenmon";
                drMonhoc.DataBind();
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
                dt.Dispose();
            }
        }

        // Nap de thi theo mon va giao vien
        // hien danh sach ket qua
        private void DS_ketqua(string mon, string lop, string ngaythi)
        {
            try
            {
                griKetqua.DataSource = new BailamBL().GetByMonAndNgay(mon,lop, ngaythi);
                griKetqua.DataBind();

            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        // hien ket qua khi an xem
        protected void btXemKQ_Click(object sender, EventArgs e)
        {
            
            string mon = drMonhoc.SelectedValue;
            string lop = txtLop.Text;
            string ngaythi = Calendar1.SelectedDate.ToShortDateString();
            //string s = ngaythi.ToLocalTime;
            lblLoi.Visible = true;
            DS_ketqua(mon, lop, ngaythi);
            /*DataTable data = new BailamBL().GetByMonAndNgay(mon, lop, ngaythi);
            //  Napmade(magv);
            if (data != null && data.Rows.Count > 0)
            {
                griKetqua.Visible = true;
                griKetqua.DataSource = data;
                griKetqua.DataBind();
                
                // Session["Tongso"] = Convert.ToInt32(data.Rows.Count.ToString());

                //txtSocauhoi.Text = Session["Socau"].ToString();
            }
            else
            {
                lblLoi.Text = "Chưa có dữ liệu!";
                lblLoi.Visible = true;
                griKetqua.Visible = false;
            }*/

        }
        protected void drMonhoc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void btChon_Click(object sender, EventArgs e)
        {
            string monhoc = drMonhoc.SelectedValue;
            string magv = Session["magv"].ToString();
        }

        protected void btexport2(object sender, EventArgs e)
        {
            ExportToExcel excel = new ExportToExcel();
            //ExportToExcel excel = new ExportToExcel();

            string mon = drMonhoc.SelectedValue;
            string lop = txtLop.Text;
            string ngaythi = Calendar1.SelectedDate.ToShortDateString();
            DataTable data = new BailamBL().GetByMonAndNgay(mon, lop, ngaythi);

            // Lấy về nguồn dữ liệu cần Export là 1 DataTable
            // DataTable này mỗi bạn lấy mỗi khác. 
            // Ở đây tôi dùng BindingSouce có tên bs nên tôi ép kiểu như sau:
            // Bạn nào gán trực tiếp vào DataGridView thì ép kiểu DataSource của
            // DataGridView nhé 




            //DataTable dt = (DataTable)bs.DataSource;

            excel.Export(data, "Danh sach", "DANH SÁCH KẾT QUẢ THI");


        }

        protected void gridCauhoi_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //string monhoc = drMonhoc1.SelectedValue;
            griKetqua.PageIndex = e.NewPageIndex;
            //this.Cauhoi_mon_gv();
            string mon = drMonhoc.SelectedValue;
            string lop = txtLop.Text;
            string ngaythi = Calendar1.SelectedDate.ToShortDateString();
            this.DS_ketqua(mon, lop, ngaythi);
        }


        



















        /*protected void btexport(object sender, EventArgs e)
        {
            string mon = drMonhoc.SelectedValue;
            string lop = txtLop.Text;
            string ngaythi = Calendar1.SelectedDate.ToShortDateString();
            DataTable dtOriginal = new BailamBL().GetByMonAndNgay(mon, lop, ngaythi);
//dtOriginal = ReturnTable(); //Return Table consisting data

//Create Tempory Table
DataTable dtTemp = new DataTable();

//Creating Header Row
dtTemp.Columns.Add("<b>Ma de thi</b>");
dtTemp.Columns.Add("<b>Ho ten</b>");
dtTemp.Columns.Add("<b>Ngay thi</b>");
dtTemp.Columns.Add("<b>Tong diem</b>");
double dSalary;
DateTime dtDate;
DataRow drAddItem;
for (int i = 0; i < dtOriginal.Rows.Count; i++)
{
drAddItem = dtTemp.NewRow();
drAddItem[0] = dtOriginal.Rows[i][0].ToString();//Name
drAddItem[1] = dtOriginal.Rows[i][6].ToString();//Email

//Join Date
dtDate = Convert.ToDateTime(dtOriginal.Rows[i][4].ToString());
drAddItem[2] = dtDate.ToShortDateString();

//Salary
dSalary = Convert.ToDouble(dtOriginal.Rows[i][3].ToString());
drAddItem[3] = dSalary.ToString();

dtTemp.Rows.Add(drAddItem);
}

//Temp Grid
DataGrid griKetqua = new DataGrid();
griKetqua.DataSource = dtTemp;
griKetqua.DataBind();
ExportToExcel("Ket_qua.xls", griKetqua);
griKetqua = null;
griKetqua.Dispose();
}

private void ExportToExcel(string strFileName, DataGrid dg)
{
    HttpContext.Current.Response.Clear();

    //HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", filename));
    
//Response.ClearContent();
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
    HttpContext.Current.Response.ContentType = "application/ms-excel";
    HttpContext.Current.Response.Charset = "UTF-8";
//Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}*/
        }
    }

